package org.wyy.vm.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.wyy.vm.entity.Video;
import org.wyy.vm.service.IVideoService;
import org.wyy.vm.service.impl.VideoServiceImpl;
import org.wyy.vm.utils.MyFileChooser;

public class VideoUpdate extends JFrame {
	
	private IVideoService videoService = new VideoServiceImpl();
	
	private static final long serialVersionUID = 6069848347444158084L;
	
	private JPanel contentPane;
	private JTextField idField;
	private JTextField nameField;
	private JTextField actorsField;
	private JTextArea contentField;
	private String imgPath;
	private String videoPath;

	/**
	 * Create the frame.
	 */
	public VideoUpdate(Video v) {
		setTitle("修改视频");
		setAlwaysOnTop(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 367, 434);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("编 号");
		lblNewLabel.setBounds(10, 35, 54, 15);
		contentPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("名 称");
		lblNewLabel_1.setBounds(10, 78, 54, 15);
		contentPane.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("演 员");
		lblNewLabel_2.setBounds(10, 126, 54, 15);
		contentPane.add(lblNewLabel_2);

		JLabel lblNewLabel_3 = new JLabel("简 介");
		lblNewLabel_3.setBounds(10, 169, 54, 15);
		contentPane.add(lblNewLabel_3);

		idField = new JTextField(""+v.getId());
		idField.setEditable(false);
		idField.setBounds(74, 32, 205, 21);
		contentPane.add(idField);

		nameField = new JTextField(v.getName());
		nameField.setBounds(74, 75, 205, 21);
		contentPane.add(nameField);

		actorsField = new JTextField(v.getActors());
		actorsField.setBounds(74, 123, 205, 21);
		contentPane.add(actorsField);

		contentField = new JTextArea(v.getContent());
		contentField.setLineWrap(true);
		contentField.setBounds(70, 164, 281, 63);
		contentPane.add(contentField);

		JButton sumbitButton = new JButton("提交");
		sumbitButton.setBounds(74, 366, 93, 23);
		sumbitButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				Long id = null;
				try {
					id = Long.valueOf(idField.getText());
				} catch (Exception e2) {
					e2.printStackTrace();
				}

				String name = nameField.getText();
				String actors = actorsField.getText();
				String content = actorsField.getText();
				
				if(id == null) {
					JOptionPane.showMessageDialog(getThis(), "编号格式不正确", "提示信息", JOptionPane.WARNING_MESSAGE);
				} else if (name == null || name.trim().equals("")) {
					JOptionPane.showMessageDialog(getThis(), "名称格式不正确", "提示信息", JOptionPane.WARNING_MESSAGE);
				} else if (actors == null || actors.trim().equals("")) {
					JOptionPane.showMessageDialog(getThis(), "演员格式不正确", "提示信息", JOptionPane.WARNING_MESSAGE);
				} else if (content == null || content.trim().equals("")) {
					JOptionPane.showMessageDialog(getThis(), "简介格式不正确", "提示信息", JOptionPane.WARNING_MESSAGE);
				} else if (imgPath == null || imgPath.trim().equals("")) {
					JOptionPane.showMessageDialog(getThis(), "图片路径不正确", "提示信息", JOptionPane.WARNING_MESSAGE);
				} else if (videoPath == null || videoPath.trim().equals("")) {
					JOptionPane.showMessageDialog(getThis(), "视频路径不正确", "提示信息", JOptionPane.WARNING_MESSAGE);
				} else {
					
					v.setName(name);
					v.setActors(actors);
					v.setContent(content);
					v.setCoverImgPath(imgPath);
					v.setVideoPath(videoPath);
					videoService.updateVideo(v);
					close();
				}

			}
		});
		contentPane.add(sumbitButton);

		JButton cancelButton = new JButton("取消");
		cancelButton.setBounds(198, 366, 93, 23);
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		contentPane.add(cancelButton);

		JLabel lblNewLabel_4 = new JLabel("图片路径");
		lblNewLabel_4.setBounds(10, 247, 64, 27);
		contentPane.add(lblNewLabel_4);

		JLabel lblNewLabel_5 = new JLabel("视频路径");
		lblNewLabel_5.setBounds(10, 295, 54, 15);
		contentPane.add(lblNewLabel_5);
		
		JButton btnNewButton = new JButton("选择封面路径");
		btnNewButton.setBounds(74, 249, 205, 23);
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				imgPath = MyFileChooser.fileUpload(getThis());
				btnNewButton.setText(imgPath);
			}
		});
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("选择视频路径");
		btnNewButton_1.setBounds(74, 291, 205, 23);
		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				videoPath = MyFileChooser.fileUpload(getThis());
				btnNewButton_1.setText(videoPath);
			}
		});
		contentPane.add(btnNewButton_1);
	}

	private JFrame getThis() {
		return this;
	}
	
	private void close() {
		dispose();
		VideoList videoList = new VideoList();
		videoList.setVisible(true);
	}
}
